import com.troyworks.particleengine.Particle;
import com.troyworks.framework.ui.layout.Bounds2;
import flash.geom.Rectangle;
import com.troyworks.framework.ui.DraggableClip;
import com.troyworks.particleengine.PhysicsParticle;
/**
 * @author Troy Gardner
 */
class com.troyworks.particleengine.Ch08_14 extends MovieClip {

	
	var bounds : Rectangle;
	
	var particles:Array = new Array();
	var clips:Array = new Array();
	
	var numOfParticles:Number = 2;
	
	//////////Forces ///////////
	var easing : Number = .1;
	var bounce : Number = 0.7;
	var gravity : Number = 1;
	var spring : Number = 0.1;
	var springLength:Number = 75;
	var friction : Number = 0.97;

	public function Ch08_14() {
		trace("Ch08_14");
	}
	public function onLoad() : Void{
		trace("onLoad");
		bounds = new Rectangle();
		bounds.top = 0;
		bounds.left = 0;
		bounds.bottom =_height;
		bounds.right = _width;

		for (var i : Number = 0; i <numOfParticles; i++) {
			var p:PhysicsParticle = new PhysicsParticle();
			p.cp.x = (Math.random() * _width) -_width/2;
			p.cp.y = (Math.random() * _height) -_height/2;
			
			p.v.x = Math.random() * 10 -5;
			p.v.y = Math.random() * 10 -5;
		
			
			particles.push(p);
			var initO:Object = new Object();
			initO._x = p.x;
			initO._y = p.y;
			initO.p = p;
			var _mc:MovieClip = attachMovie("Ball","Ball"+i, i+10, initO);
			p.view = _mc;
			clips.push(_mc);
		}
	}
	function springTo(pA:PhysicsParticle, pB:PhysicsParticle):Void{
			trace("springTo");
			var dx:Number = pA.x - pB.x;
			var dy:Number = pA.y - pB.y;
			var angle:Number = Math.atan2(dy, dx);
			var targetX:Number = pB.x + Math.cos(angle) * springLength;
			var targetY:Number = pB.y + Math.sin(angle) * springLength;
			pA.v.x  += (targetX - pA.x) * spring;
			pA.v.y  += (targetY - pA.y) * spring;
			pA.v.x *= friction;
			pA.v.y *= friction;
	}
	function gravitate(pA:PhysicsParticle, pB:PhysicsParticle):Void{
			trace("gravitate");
			var dx:Number = pA.x - pB.x;
			var dy:Number = pA.y - pB.y;
			var distSQ:Number = dx*dx + dy*dy;
			var dist:Number = Math.sqrt(distSQ);
			var force:Number = pA.mass * pB.mass /distSQ;
			var ax:Number = force * dx /dist;
			var ay:Number = force * dy /dist;
			pA.v.x += ax /pA.mass;
			pA.v.y += ay/ pA.mass;
			pB.v.x += ax /pB.mass;
			pB.v.y += ay/ pB.mass;
	}
	function applyGravity(p:PhysicsParticle):Void{
		p.v.y += gravity;
	}
	function checkBounds(p:PhysicsParticle):Void{
		var ball:DraggableClip = DraggableClip(p.view);
		var b : Bounds2 = new Bounds2(ball.getBounds(this));
			///////// check x ///////////////
			if(b.xMax > bounds.right){
					trace("hit R wall");
					p.cp.x = bounds.right - b.width;
					p.v.x = Math.abs(p.v.x)*-1* bounce;
			}else if(b.xMin < bounds.left){
					trace("hit L wall");
					p.cp.x = bounds.left + 1;// + b.width + 1;
					p.v.x = Math.abs(p.v.x) * bounce;
			}
			////////// check y ////////////////
			if( bounds.bottom -.9 < b.yMax && b.yMax < bounds.bottom +.9 && Math.abs(p.v.y) <1){
		//		trace("on Bottom");
			//	v.x = 0;
			      if(gravity >0){
					p.v.y = -gravity;
			      }
			} else if(b.yMax >bounds.bottom){
					trace("hit Bottom yMax: " + b.yMax  + " bottom: " + bounds.bottom + " v: " + p.v.y);
	
					p.v.y= Math.abs(p.v.y* bounce)*-1;
					p.cp.y -= (b.yMax- bounds.bottom) - p.v.y;
			}else if(b.yMin < bounds.top){
					trace("hit Top");
					p.cp.y = bounds.top +1 ;//+ b.height;
					p.v.y= Math.abs(p.v.y* bounce);
			}
			///// update the clip /////////
			ball._x = p.x;
			ball._y = p.y;
	}
	function onEnterFrame() : Void {
		springTo(particles[0], particles[1]);
		springTo(particles[1], particles[0]);
		if(gravity > 0){
		applyGravity(particles[0]);
		applyGravity(particles[1]);
		}
		checkBounds(particles[0]);
		checkBounds(particles[1]);
	
					//////////// draw the line ///////////
				clear();
				lineStyle(1, 0, 100);
			//	var b1:Bounds2 = new Bounds(
				moveTo(particles[0].x,particles[0].y);
				lineTo(particles[1].x,particles[1].y);
			
		
	}
}